const InfoApi = require('../../api/information.js')
const util = require('../../utils/util.js')
const commonUtil = require('../../utils/common.js')

Page({
  data: {
    currentMenuName: '铜',
    currentPageId: '1713818998033260545',
    pageList: [
      {itemName: '铜', pageId: '1713818998033260545'},
      {itemName: '铝', pageId: '1713819084322676737'},
      {itemName: '锌', pageId: '1713819185438957569'},
      {itemName: '铅', pageId: '1713819279378784257'},
      {itemName: '锡', pageId: '1713819359552905217'},
      {itemName: '镍', pageId: '1713819446073008130'},
    ],
    pagination: {
      current: 1,
      pageSize: 20,
      total: 0
    },
    pageInfo: {},
    tableData: [],
    hasMore: false,
    optionsData: {
      currentMenuName: null,
      currentPageId: null,
    },
    lauOptions: {},
  },
  onLoad(options){
    console.log(options)
    if (options.currentMenuName) {
      this.setData({
        ['optionsData.currentMenuName']: options.currentMenuName,
        ['optionsData.currentPageId']: options.currentPageId,
      })
    }
  },

  onShow() {
    if (typeof this.getTabBar === 'function' &&  this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3
      })
    }
    const _lauOptions = wx.getLaunchOptionsSync();
    this.setData({
      lauOptions: _lauOptions
    })
    this.removeStorage()

    if (this.data.optionsData.currentMenuName) {
      this.setData({
        currentMenuName: this.data.optionsData.currentMenuName,
        currentPageId: this.data.optionsData.currentPageId,
      })
      setTimeout(()=>{
        this.getFrontLatestArticleList()
      }, 200)
    } else {
      let detailBack = wx.getStorageSync('detailBack');
      let currentMenuInfo = wx.getStorageSync('currentMenuInfo');
      console.log(detailBack)
      console.log(currentMenuInfo)
      if (!detailBack) {
        this.initData()
        setTimeout(()=>{
          this.getFrontLatestArticleList()
        }, 200)
      } else {
        if (currentMenuInfo) {
          this.setData({
            currentMenuName: currentMenuInfo.itemName,
            currentPageId: currentMenuInfo.pageId,
          })
        }
        if (this.data.tableData.length == 0) {
          setTimeout(()=>{
            this.getFrontLatestArticleList()
          }, 200)
        }
      }
    }
  },
  onHide(){
    let _newsInfo = wx.getStorageSync('newsInfo');
    if (!_newsInfo) {
      this.initData()
      setTimeout(()=>{
        this.getFrontLatestArticleList()
      }, 200)
      wx.removeStorageSync('detailBack')
      wx.removeStorageSync('currentMenuInfo')
    }
  },
  initData(){
    this.setData({
      currentMenuName: '铜',
      currentPageId: '1713818998033260545',
      ['optionsData.currentMenuName']: null,
      ['optionsData.currentPageId']: null,
    })
  },
  removeStorage(){
    // wx.removeStorageSync('currentQuote');
    wx.removeStorageSync('currentMenu');
    wx.removeStorageSync('currentSub');
    // wx.removeStorageSync('detailBack');
  },
  // 查询前端最新文章组件
  getFrontLatestArticleList(){
    InfoApi.getFrontLatestArticleList({pageId: this.data.currentPageId, locationId: '1714197680119816193'}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        if (res.data && res.data.length > 0) {
          this.setData({
            pageInfo: res.data[0]
          })
          this.getFrontLatestArticleNewsPage(res.data[0].latestArticleId)
        } else {
          this.setData({
            pageInfo: {},
            tableData: [],
            hasMore: false,
          })
        }
      } else {
        if (res.errCode == '999901') {
          commonUtil.handleTokenExpire('/pages/news/news','tab')
        }
      }
    })
  },
  // 查询前端最新文章组件新闻
  getFrontLatestArticleNewsPage(_latestArticleId){
    InfoApi.getFrontLatestArticleNewsPage({latestArticleId: _latestArticleId, pageNum: this.data.pagination.current, pageSize: this.data.pagination.pageSize}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        let array = [];
        if (res.data.data && res.data.data.length > 0) {
          array = res.data.data.map(item=>{
            if (item.columnPrefix && item.newsId) {
              item.itemLink = `http://news.tongdow.com/${item.columnPrefix}/${item.newsId}`
            }
            item.publishTime = item.publishTime ? util.getDateDiff(item.publishTime) : '';
            return item;
          })
        }
        if (this.data.pagination.current == 1) {
          this.setData({
            tableData: array
          })
        } else {
          this.setData({
            tableData: this.data.tableData.concat(array)
          })
        }
        this.setData({
          ['pagination.total']: res.data.count,
          hasMore: res.data.pages > this.data.pagination.current ? true : false
        })
        let detailBack = wx.getStorageSync('detailBack');
        if (detailBack) {
          wx.removeStorageSync('detailBack')
        }
      }
    })
  },
  // 点击菜单
  menuClick(e){
    let current = e.currentTarget.dataset.item;
    wx.setStorageSync('currentMenuInfo', current);
    this.setData({
      currentMenuName: current.itemName,
      currentPageId: current.pageId,
      ['pagination.current']: 1,
    })
    this.getFrontLatestArticleList()
  },
  // 查看新闻详情
  checkDetail(e){
    let currentItem = e.currentTarget.dataset.item;
    // console.log(currentItem);
    if (this.data.lauOptions && this.data.lauOptions.scene == 1154) {
      // 微信朋友圈中打开小程序
      wx.switchTab({
        url: '/pages/news/news',
      })
    } else {
      wx.removeStorageSync('newsInfo');
      setTimeout(()=>{
        wx.setStorageSync('newsInfo', currentItem)
        wx.navigateTo({
          url: '/packageA/pages/newsDetail/index',
        })
      }, 200)
    }
  },
  // 加载更多
  showMore(){
    var pageNum = this.data.pagination.current;
    this.setData({
      ['pagination.current']: pageNum+1
    })
    this.getFrontLatestArticleList(this.data.pageInfo.latestArticleId)
  },

  onShareAppMessage() {
    console.log(this.data.currentMenuName, this.data.currentPageId)

    return {
      title: "资讯",
      path: "/pages/news/news?currentMenuName="+this.data.currentMenuName+"&currentPageId="+this.data.currentPageId,
    }
  },

  onShareTimeline() {
    return {
      title: "资讯",
      query: "currentMenuName="+this.data.currentMenuName+"&currentPageId="+this.data.currentPageId,
    }
  }
})